<template>
	<view class="pwd">
		<!-- TODO修改密码 -->
		<u--form :model="changepwd" ref="uForm" :rules="rules" labelPosition="top" labelWidth="auto"
			:labelStyle="{fontWeight:'bold',marginBottom:'20rpx'}">
			<!--  电话号码-->
			<u-form-item prop="phoneNumber" label="电话号码">
				<u--input v-model="changepwd.phoneNumber" placeholder="请输入电话号码" border="surround" clearable></u--input>
			</u-form-item>
			<!-- 输入密码 -->
			<u-form-item prop="password" label="密码">
				<u--input v-model="changepwd.password" placeholder="请输入密码" border="surround" clearable></u--input>
			</u-form-item>

			<!-- 输入新密码 -->
			<u-form-item prop="newPassword" label="新密码">
				<u--input v-model="changepwd.newPassword" placeholder="请输入新密码" border="surround" clearable></u--input>
			</u-form-item>
			<!-- <u-button @click="submit" type="primary" text="提交"></u-button> -->
		</u--form>
		<view class="btn" @click="submit">提交</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				changepwd: {
					newPassword: "",
					password: "",
					phoneNumber: ""
				},
				rules: {
					'password': {
						type: 'string',
						required: true,
						message: '请填写密码',
						// 不希望表单重置后还有提示，去掉change
						trigger: ['blur']
					},
					'phoneNumber': {
						// type: 'number',
						type: 'string',
						required: true,
						// 上面两个都会校验只要一个不满足就会提示message 不为数字类型 没填（为空） 都会提示
						message: '请填写电话号码',
						trigger: ['blur']
					},
					'newPassword': {
						type: 'string',
						required: true,
						message: '请填写新密码',
						trigger: ['blur']
					},
				},
			}
		},
		methods: {
			// 1.提交按钮 校验表单是否为空
			submit() {
				// 1.ref 配合 2.this.$refs.xx.....检验表单是否填写完 2.validateField()对某个字段校验 3.validate()校验所有字段
				this.$refs.uForm.validate().then(res => {
					// uni.$u.toast('校验通过')
					this.changePassword()

				}).catch(errors => {

					// uni.$u.toast('请完整填写所有内容')
					uni.showToast({
						icon: 'error',
						title: '请完整填写所有内容',

					})
				})
			},
			// 2.修改密码的方法 19827352498 123
			changePassword() {
				uni.request({
					url: "http://blog.coldworld.top:8081/user/updatePwd",
					method: "POST",
					data: this.changepwd,
					success: (res) => {
						// console.log(res)
						let msg = res.data.message
						if (res.data.code == 20000) {
							uni.$u.toast('修改成功')
							uni.removeStorageSync('id')
							setTimeout(() => {
								// 1s后重定向到登录页
								uni.redirectTo({
									url: "../login/login"
								})
							}, 1500)
							this.$refs.uForm.resetFields()
						} else {
							uni.showToast({
								title: msg,
								icon: "error",
							})
						}
					}
				})
			},

		},
		onLoad() {
			let id = uni.getStorageSync('id')
			if (id) {
				return;
			} else {
				uni.showToast({
					title: "请先登录",
					icon: "error"
				})

				setTimeout(() => {
					// 1s后重定向到登录页
					uni.redirectTo({
						url: "../login/login"
					})
				}, 1500)
			}
		}
	}
</script>

<style lang="scss">
	.pwd {
		padding: 20rpx;

		.btn {
			height: 100rpx;
			border-radius: 10rpx;
			background-color: #1269db;
			color: #fff;
			text-align: center;
			line-height: 80rpx;
			margin-top: 20rpx;
		}

	}
</style>
